package com.hi.hiparty.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class WebUtil{
  public static final DateFormat dateFormat = new SimpleDateFormat(CalendarUtil.TIME_PATTERN_M);
  
  public static int getIntPara(HttpServletRequest request, String name)
  {
    return getIntPara(request, name, 0);
  }

  public static Date getDateValue(HttpServletRequest request, DateFormat format,String name)
  {
    return getDateValue(request,format, name, null);
  }
  
  public static Date getDateValue(HttpServletRequest request,DateFormat format,String name, Date defaultValue) {
	  String value = request.getParameter(name);
      Date  date = defaultValue;
      if (value != null) {
          try {
              format.setLenient(false);
              date = format.parse(value);
          } catch (Exception e) {
              date = defaultValue;
          }
      }
      return date;
  }
  
  public static Double getDoubleValue(HttpServletRequest request,String name){
	  return getDoubleValue(request,name,0.0);
  }
  public static Double getDoubleValue(HttpServletRequest request,String name, Double defaultValue){
	  String value = request.getParameter(name);
	   try{
	       return Double.parseDouble(value); 
	   }catch(Exception e){
	   } 
	  return defaultValue;
  }
  
	public static int getIntPara(HttpServletRequest request, String name,int defaultVal) {
		String value = request.getParameter(name);
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {
		}
		return defaultVal;
	}

  public static long getLongPara(HttpServletRequest request, String name)
  {
    return getLongPara(request, name, 0L);
  }

  public static long getLongPara(HttpServletRequest request, String name, long defaultVal)
  {
    String value = request.getParameter(name);
    try {
      return Long.parseLong(value); 
      } catch (Exception e) {
    }
    return defaultVal;
  }

  public static String[] getStringsPara(HttpServletRequest request, String name){
	  return request.getParameterValues(name);
  }
  
  public static String getStringPara(HttpServletRequest request, String name)
  {
	 return getStringPara(request,name,"");
  }
  
  public static String getStringPara(HttpServletRequest request, String name,String defaultVal)
  {
    String value = request.getParameter(name);
    if (value == null) return defaultVal;
    return value.trim();
  }

  public static String getRemoteIP(HttpServletRequest request) {
    try {
      String ip = request.getHeader("X-Forwarded-For");
      if (ip == null) ip = request.getRemoteHost();
      if (ip == null) ip = "127.0.0.1";
      return ip; } catch (Exception exp) {
    }
    return "127.0.0.1";
  }
  //增加一个方法，处理文件类型，获取fileItem，此方法适合单图片。
  @SuppressWarnings("unchecked")
  public static FileItem getFileItem(HttpServletRequest request,String name,int sizeThreshold){
	  DiskFileItemFactory factory = new DiskFileItemFactory();
	  factory.setSizeThreshold(sizeThreshold);
	  ServletFileUpload upload = new ServletFileUpload();
	  upload.setFileItemFactory(factory);
	  List<FileItem> fList = null;
	  try {
		  fList = (List<FileItem>)upload.parseRequest(request);
		  Iterator<FileItem> it = fList.iterator();
          while (it.hasNext()) {
              FileItem item = it.next();
              if (!item.isFormField()) {    //是文件字段表单字段
            	    if(item.getFieldName().equalsIgnoreCase(name)){
            	    	return item;
            	    }
              }
          }
	  }catch(Exception e){
		  return null;
	  }
	  return null;
  }
}

